package com.baozheyangken.ai_learn.controller;

import com.baozheyangken.ai_learn.annotation.RequiresAuthentication;
import com.baozheyangken.ai_learn.pojo.dto.UserDTO;
import com.baozheyangken.ai_learn.pojo.dto.UserLoginDTO;
import com.baozheyangken.ai_learn.pojo.entity.User;
import com.baozheyangken.ai_learn.pojo.result.Result;
import com.baozheyangken.ai_learn.pojo.vo.UserLoginVO;
import com.baozheyangken.ai_learn.pojo.vo.UserVO;
import com.baozheyangken.ai_learn.properties.JwtProperties;
import com.baozheyangken.ai_learn.service.UserService;
import com.baozheyangken.ai_learn.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/auth")
@Slf4j
@RequiresAuthentication
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 注册
     *
     * @param userVO
     * @return
     */
    @PostMapping("/register")
    public Result<UserDTO> register(@RequestBody UserVO userVO) {
        log.info("用户注册信息:{}", userVO);
        return userService.userRegister(userVO);
    }

    /**
     * 登入
     *
     * @param userLoginVO
     * @return
     */
    @PostMapping("/login")
    public Result<UserLoginDTO> login(@RequestBody UserLoginVO userLoginVO) {
        log.info("用户登入信息:{}", userLoginVO);
        User user = userService.userLogin(userLoginVO);

        //生成jwt
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", user.getId());

        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey().getBytes(StandardCharsets.UTF_8),//密钥
                jwtProperties.getUserTtl(), //过期时间
                claims
        );

        UserLoginDTO userLoginDTO = UserLoginDTO.builder()
                .token(token)
                .userDTO(
                        UserDTO.builder()
                                .id(user.getId())
                                .username(user.getUsername())
                                .email(user.getEmail())
                                .build()
                )
                .build();

        return Result.success(userLoginDTO);
    }

     /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("/users")
    public Result<User> info() {
        return Result.success(userService.userInfo());
    }

    /**
     * 修改当前用户信息
     *
     * @param email
     * @param password
     * @param oldPassword
     * @return
     */
    @PutMapping("/users")
    public Result<UserDTO> update(
            @Param("id") Integer id,
            @Param("username") String username,
            @Param("email") String email,
            @Param("password") String password,
            @Param("oldPassword") String oldPassword) {
        return userService.userUpdate(id,username,email, password, oldPassword);
    }

    @DeleteMapping("/users/{id}")
    public Result delete(@PathVariable Integer id) {
        return userService.userDelete(id);
    }
}
